package com.example.mobilesafe.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mobilesafe.R;
import com.example.mobilesafe.constant.ConstantValue;
import com.example.mobilesafe.utils.MD5Util;
import com.example.mobilesafe.utils.SharedPreferencesUtil;
import com.example.mobilesafe.utils.ToastUtil;

public class HomeActivity extends AppCompatActivity {

    /**
     * 存储标题
     */
    private String[] mTitleStrs;

    /**
     * 存储图像
     */
    private int[] mDrawableIds;

    /**
     * 网格对象
     */
    private GridView gv_home;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        
        // 初始化UI
        initUI();

        // 初始化数据
        initData();
    }

    /**
     * 1.初始化UI
     */
    private void initUI() {
        gv_home = findViewById(R.id.gv_home);
    }

    /**
     * 2.初始化数据
     */
    private void initData() {
        // 1.初始化每个图标的标题
        mTitleStrs = new String[]{"手机防盗","通信卫士","软件管理","进程管理","流量统计","手机杀毒","缓存清理","高级工具","设置中心"};
        // 2.初始化每个图标的图像
        mDrawableIds = new int[]{R.drawable.home_safe,R.drawable.home_callmsgsafe,R.drawable.home_apps,R.drawable.home_taskmanager,R.drawable.home_netmanager,R.drawable.home_trojan,R.drawable.home_sysoptimize,R.drawable.home_tools,R.drawable.home_settings};
        // 3.为GridView设置数据适配器
        gv_home.setAdapter(new MyAdapter());
        // 4.注册GridView中单个条目的点击事件
        gv_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        // 手机防盗
                        showDialog();
                        break;
                    case 1:
                        // 通信卫士
                        startActivity(new Intent(getApplicationContext(),BlackNumberActivity.class));
                        break;
                    case 2:
                        // 软件管理
                        startActivity(new Intent(getApplicationContext(),AppManagerActivity.class));
                        break;
                    case 3:
                        // 进程管理
                        startActivity(new Intent(getApplicationContext(),ProcessManagerActivity.class));
                        break;
                    case 4:
                        // 流量统计
                        startActivity(new Intent(getApplicationContext(),TrafficActivity.class));
                        break;
                    case 5:
                        // 手机杀毒
                        startActivity(new Intent(getApplicationContext(),AnitVirusActivity.class));
                        break;
                    case 6:
                        // 缓存清理
                        // startActivity(new Intent(getApplicationContext(),CacheCleanActivity.class));
                        startActivity(new Intent(getApplicationContext(),BaseCacheCleanActivity.class));
                        break;
                    case 7:
                        // 高级工具
                        startActivity(new Intent(getApplicationContext(),AToolActivity.class));
                        break;
                    case 8:
                        // 设置中心
                        Intent intent = new Intent(getApplicationContext(), SettingActivity.class);
                        startActivity(intent);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    /**
     * 3.自定义的数据适配器类
     */
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // 统计条目的总数
            return mTitleStrs.length;
        }

        @Override
        public Object getItem(int position) {
            // 根据索引获取对象
            return mTitleStrs[position];
        }

        @Override
        public long getItemId(int position) {
            // 获取索引
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 获取视图
            View view = View.inflate(getApplicationContext(), R.layout.gridview_item, null);
            TextView tv_title = view.findViewById(R.id.tv_title);
            ImageView iv_icon = view.findViewById(R.id.iv_icon);
            tv_title.setText(mTitleStrs[position]);
            iv_icon.setBackgroundResource(mDrawableIds[position]);
            return view;
        }
    }

    /**
     * 4.手机防盗——密码对话框
     */
    private void showDialog() {
        // 1.通过判断本地是否有存储密码来确定显示哪个对话框（sp）
        String password = SharedPreferencesUtil.getString(this, ConstantValue.MOBILE_SAFE_PASSWORD, "");
        if (TextUtils.isEmpty(password)){
            // 2.初始设置密码对话框
            showSetPasswordDialog();
        }else {
            // 3.确认密码对话框
            showConfirmPasswordDialog();
        }
    }

    /**
     * 5.初次设置密码对话框
     */
    private void showSetPasswordDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        final View view = View.inflate(this, R.layout.dialog_set_password, null);
        dialog.setView(view);
        dialog.show();
        Button btn_submit = view.findViewById(R.id.btn_submit);
        Button btn_cancel = view.findViewById(R.id.btn_cancel);
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et_set_password = view.findViewById(R.id.et_set_password);
                EditText et_confirm_password = view.findViewById(R.id.et_confirm_password);
                String password = et_set_password.getText().toString();
                String confirmpassword = et_confirm_password.getText().toString();
                if (!TextUtils.isEmpty(password) && !TextUtils.isEmpty(confirmpassword)){
                    if(password.equals(confirmpassword)){
                        // 进入手机防盗模块
                        Intent intent = new Intent(getApplicationContext(), SetupOverActivity.class);
                        startActivity(intent);
                        dialog.dismiss();
                        // 将密码存储到sp中
                        SharedPreferencesUtil.putString(getApplicationContext(),ConstantValue.MOBILE_SAFE_PASSWORD, MD5Util.encoder(password));
                    }else {
                        // 提示用户确认密码有误
                        ToastUtil.show(getApplicationContext(),"确认密码错误");
                    }
                }else {
                    // 提示用户密码输入有为空
                    ToastUtil.show(getApplicationContext(),"请输入密码");
                }
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    /**
     * 6.再次设置密码对话框
     */
    private void showConfirmPasswordDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        final View view = View.inflate(this, R.layout.dialog_confirm_password, null);
        dialog.setView(view);
        dialog.show();
        Button btn_submit = view.findViewById(R.id.btn_submit);
        Button btn_cancel = view.findViewById(R.id.btn_cancel);
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et_confirm_password = view.findViewById(R.id.et_confirm_password);
                String confirmpassword = et_confirm_password.getText().toString();
                if (!TextUtils.isEmpty(confirmpassword)){
                    // 从sp中获取加密过后的密码，然后将输入的密码同样进行MD5算法，再进行比对
                    String password = SharedPreferencesUtil.getString(getApplicationContext(), ConstantValue.MOBILE_SAFE_PASSWORD, "");
                    if(password.equals(MD5Util.encoder(confirmpassword))){
                        // 进入手机防盗模块
                        Intent intent = new Intent(getApplicationContext(), SetupOverActivity.class);
                        startActivity(intent);
                        dialog.dismiss();
                    }else {
                        // 提示用户确认密码有误
                        ToastUtil.show(getApplicationContext(),"确认密码错误");
                    }
                }else {
                    // 提示用户密码输入有为空
                    ToastUtil.show(getApplicationContext(),"请输入密码");
                }
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
}
